﻿// -----------------------------------------------------------------------
// <copyright file="Pushover.cs" company="The Usual Dosage">
// © 2013 by Matthew M. Martin
// </copyright>
// -----------------------------------------------------------------------


namespace Cadence.NET.Notifications
{
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Linq;
    using System.Net;
    using Cadence.NET.Interfaces;

    /// <summary>
    /// Sends a push notification via Pushover.
    /// </summary>
    /// <remarks>
    /// For this class to work properly, a valid application toke and user token need to be obtained
    /// from Pushover. Pushover is free for personal use.
    /// </remarks>
    /// <seealso cref="https://pushover.net/"/>
    public class Pushover : INotificationService
    {
        #region Properties

        /// <summary>
        /// Gets or sets the app key.
        /// </summary>
        /// <value>
        /// The app key.
        /// </value>
        /// <remarks>This should be "PushoverAppKey" in the web/app config.</remarks>
        private string pushoverAppKey = string.Empty;
        public string PushoverAppKey
        {
            get
            {
                pushoverAppKey = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["PushoverAppKey"])) ? ConfigurationManager.AppSettings["PushoverAppKey"] : null;
                return pushoverAppKey;
            }
            set
            {
                pushoverAppKey = value;
            }
        }

        /// <summary>
        /// Gets or sets the user tokens.
        /// </summary>
        /// <value>
        /// The user tokens.
        /// </value>
        /// <remarks>This should be "PushoverUserTokens" in the web/app config.</remarks>
        private List<string> userTokens = null;
        public List<string> PushoverUserTokens
        {
            get
            {
                userTokens = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["PushoverUserTokens"])) ? ConfigurationManager.AppSettings["PushoverUserTokens"].Split(',').ToList() : null;
                return userTokens;
            }
            set
            {
                userTokens = value;
            }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Sends a push message via Pushover.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public bool Send(string message)
        {
            try
            {
                if (PushoverUserTokens != null)
                {
                    PushoverUserTokens.ForEach(t => {
                        var parameters = new NameValueCollection {
                            { "token", PushoverAppKey },
                            { "user", t },
                            { "message", message }
                        };
                        using (var client = new WebClient())
                        {
                            var result = client.UploadValues("https://api.pushover.net/1/messages.json", parameters);
                        }
                    });
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion Methods
    }
}
